package com.lq.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lq.product.entity.Product;
import com.lq.product.mapper.ProductMapper;
import com.lq.product.service.IProductService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author lq
 * @since 2025-08-23
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Override
    public List<Product> getByCategoryId(Long categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        return list(queryWrapper);
    }

    @Override
    public Page<Product> listProductsByPage(Page<Product> page) {
        return page(page);
    }

    @Override
    public boolean updateStock(Long productId, Integer quantity) {
        Product product = getById(productId);
        if (product != null) {
            product.setStock(quantity);
            product.setUpdateTime(LocalDateTime.now());
            return updateById(product);
        }
        return false;
    }

    @Override
    public boolean putOnShelf(Long productId) {
        Product product = getById(productId);
        if (product != null) {
            product.setStatus(1);
            product.setUpdateTime(LocalDateTime.now());
            return updateById(product);
        }
        return false;
    }

    @Override
    public boolean putOffShelf(Long productId) {
        Product product = getById(productId);
        if (product != null) {
            product.setStatus(0);
            product.setUpdateTime(LocalDateTime.now());
            return updateById(product);
        }
        return false;
    }
}